<?php
namespace app\api\controller\goods;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\orderdata\Goods;
use app\common\model\orderhawa\Hawa;
use app\common\model\orderreceipt\ReceiptList;
use app\api\controller\Base;
use app\common\model\power\Rolepower;
use app\common\model\orderdata\StorageAreaGoods;
class Goodsweight extends Base
{
	/**
	 * 用户登录后的公用方法
	 */
    public function initialize(){
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(202003);
	}

	/**
	 *	获取库存货物
	 * @return [type] [description]
	 */
	public function getgoodlist(){
		$goodlist = Goods::getwarelist();
		$goodlist = $goodlist->toArray();
		$good_id = array_column($goodlist, 'GoodId');
		$area = StorageAreaGoods::where('GoodId', 'in', $good_id)
			->alias('a')
			->leftJoin(['storagearea'=> 'b'], 'a.StorageAreaId = b.StorageAreaId')
			->where('b.State', 1)
			->field('a.*,b.Name,b.GroupName')
			->select();
		$data = [
			'goodlist' => $goodlist,
			'area' => $area
		];
		return myok($data);
	}
	/**
	 * 获取区域/代码的入库详情
	 * @return [type] [description]
	 */
	public function getinfo(){
		$data = input('post.');
		if(empty($data['Type'])){return myerr('缺少分类信息');}
		$wait_receiptlist_order_id = ReceiptList::where('State',ReceiptList::STATEWAIT)->column('ReceiptListId');
		switch ($data['Type']) {
			case 'area':
				//库存区域模式
				$res = Hawa::where('StorageAreaId',$data['StorageAreaId'])->where('GoodId',$data['GoodId'])->where('GoodName',$data['GoodName'])->with(['listinfo','listinfo'=>['companyinfo','receiptinfo']])->order('HawaId desc')->limit(100)->select();
				break;
			case 'good':
				// 货物模式
				$res = Hawa::where('GoodId',$data['GoodId'])->where('GoodName',$data['GoodName'])->with(['listinfo','listinfo'=>['companyinfo','receiptinfo']])->order('HawaId desc')->limit(100)->select();
				break;
			
			default:
				return myerr('缺少分类信息');
				break;
		}
		$res = $res->visible(['ReceiveWeight','Weight','HawaListId','listinfo'=>['ArriveTime','OrderNumber','companyinfo.Name','receiptinfo'=>['WarehouseTime','ReceiptListId']]])->toArray();
		foreach ($res as $key => $value) {
			if(in_array($value['listinfo']['receiptinfo']['ReceiptListId'],$wait_receiptlist_order_id)){
				unset($res[$key]);
			}
		}
		return myok($res);
	}
	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}
}
